package com.ten.mall.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ten.mall.service.CartService;
import com.ten.mall.service.ItemOrderService;
import com.ten.mall.service.ItemService;
import com.ten.mall.service.LunboService;
import com.ten.mall.vo.Cart;
import com.ten.mall.vo.Item;
import com.ten.mall.vo.ItemOrder;
import com.ten.mall.vo.Lunbo;
import com.ten.mall.vo.User;
@Controller
public class PhoneController {
	@Autowired
	private ItemService itemService;
	@Autowired
	private LunboService lunboService;
	@Autowired
	private ItemOrderService itemOrderService;
	@Autowired
	private CartService cartService;
	@RequestMapping("/phone/shop")
	public String index(@RequestParam(name="start",defaultValue="1") Integer start,
            			@RequestParam(name="size",defaultValue="100") Integer size,
            			HttpServletRequest model) throws Exception{ 
		/*String myName="";
		if(StringUtils.isNotBlank(name)){
				byte[] bytes=name.getBytes("ISO-8859-1");
				myName=new String(bytes,"utf-8");
		}*/
		List<Lunbo> plist =lunboService.selectAll();
		List<Item> list =itemService.selectByItemVo(start, size, 1);
		model.setAttribute("list", list);
		model.setAttribute("plist", plist);
		return "phone/phone";
	}
	@RequestMapping("/phone/detail")
	public String detail(Integer id,HttpServletRequest  res){
		Item item=itemService.selectById(id);
		User u =(User) res.getSession().getAttribute("u");
		int cartNum =0;
		if(u!=null){
			List<Cart> list =cartService.selectByPages(1, 10000, u.getId());
			if(list!=null&&list.size()>0){
				cartNum =list.size();
			}
		}
		
		String str[] =item.getGoodsRemark().split(",");
		List<String> list =new ArrayList<>();
		for(String s:str){
			list.add(s);
		}
		res.setAttribute("cartNum", cartNum);
		res.setAttribute("item", item);
		res.setAttribute("list", list);
		return "phone/shop_detail";
	}
	@RequestMapping("/phone/category")
	public String tocategory(){
		return "/phone/category";
	}
	@RequestMapping("/phone/userInfo")
	public String toUserInfo(HttpServletRequest res){
		User u =(User) res.getSession().getAttribute("u");
		int noPay =0;
		int noShen =0;
		int noTui =0;
		int noTake =0;
		if(u!=null&&StringUtils.isNotBlank(u.getId())){
			List<ItemOrder> list= itemOrderService.listItemOrder(u.getId(),1, 100000);
			for(ItemOrder i:list){
				if(i.getOrderState()==1){
					noPay++;
				}else if(i.getOrderState()==2){
					noShen++;
				}else if(i.getOrderState()==8||i.getOrderState()==9){
					noTui++;
				}else if(i.getOrderState()>=3&&i.getOrderState()<6){
					noTake++;
				}
			}
		}
		res.setAttribute("noPay", noPay);
		res.setAttribute("noShen", noShen);
		res.setAttribute("noTui", noTui);
		res.setAttribute("noTake", noTake);
		return "/phone/user_info";
	}
	//phone/payOk
	@RequestMapping("/phone/payOk")
	public String payOk(){
		return "/phone/payOk";
	}
	@RequestMapping("/phone/payError")
	public String payError(){
		return "/phone/payError";
	}
	
	@RequestMapping("/error")
	public String error(){
		return "/error";
	}
	
	@RequestMapping("/phone/saleman")
	public String saleman(){
		return "phone/saleman";
	}
	@RequestMapping("/phone/dianyuan")
	public String dianyuan(){
		return "phone/dianyuan";
	}
	
	
	@RequestMapping("/phone/xiyi")
	public String xiyi(){
		return "phone/xiyi";
	}
	/**
	 * 商品搜索
	 * @param name
	 * @param res
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/phone/itemSearch")
	public String mySearch(@RequestParam(name="start",defaultValue="1") Integer start,
						   @RequestParam(name="size",defaultValue="100") Integer size,
						   String name,HttpServletRequest res) throws Exception{
		String myName="";
		if(StringUtils.isNotBlank(name)){
				byte[] bytes=name.getBytes("ISO-8859-1");
				myName=new String(bytes,"utf-8");
		}
		Map<String,Object> map =itemService.selectByPages(1, size, myName);
		List<Item> list =(List<Item>) map.get("list");
		res.setAttribute("list", list);
		return "phone/shop_search";
	}
}
